﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Components;
using UI.Lib.Factory;

namespace UI.Lib.Set
{
    public class ShopSetting
    {
        private static Dictionary<string, Dictionary<string, string>> _setting = null;

        public static void ClearAll()
        {
            _setting = null;
        }

        private static void Load()
        {
            _setting = new Dictionary<string, Dictionary<string, string>>();

            var data = CommonHelper.Entity.ExecFun("getshopsetting",
               new KeyValuePair<string, object>("comcode", Context.Comocde));

            if (data == null || data.Rows.Count == 0)
                return;

            foreach (DataRow dr in data.Rows)
            {
                var shopid = Validation.ToString(dr["shopid"]);
                var setname = Validation.ToString(dr["setname"]);
                var value = Validation.ToString(dr["value"]);

                Dictionary<string, string> dict = null;
                if (_setting.ContainsKey(shopid))
                {
                    dict = _setting[shopid];
                }
                else
                {
                    dict = new Dictionary<string, string>();
                    _setting.Add(shopid, dict);
                }

                if (dict.ContainsKey(setname))
                    dict[setname] = value;
                else
                    dict.Add(setname, value);
            }
        }

        public static string GetValue(string shopid, string setname)
        {
            if (null == _setting)
                Load();

            if (null == _setting)
                return null;

            if (_setting.ContainsKey(shopid))
            {
                var dict = _setting[shopid];

                if (dict != null && dict.ContainsKey(setname))
                    return dict[setname];
            }

            return null;
        }

        public static bool DirectStock(string shopid)
        {
            var value = GetValue(shopid, Components.Enum.ShopSetting.DirectStock);

            return "Y" == value; //默认不直接发货
        }

        public static bool NeedAudit(string shopid)
        {
            var value = GetValue(shopid, Components.Enum.ShopSetting.NeedAudit);

            return "N" != value; //默认需要审核
        }

        public static bool PrintPrice(string shopid)
        {
            var value = GetValue(shopid, Components.Enum.ShopSetting.PrintPrice);

            return "N" != value; //默认需要打印
        }
    }
}
